<script>
	$(function(){
		Namespace.register("EBCMS.[namespace]");
		EBCMS.[namespace].refresh = function refresh(){
			EBCMS.CORE.api({
				queryParams:{
					sort:'sort',
					order:'desc',
					__model:'group',
				},
				group:'group',
				tree:true,
				treelevel:true,
				tpl:'[namespace]-table',
				target:'#[namespace]_main',
				compileAfter:function(data){
					$('#[namespace]_main').find('.input_sort').focus(function(){
						EBCMS.[namespace].sortvalue = $(this).val();
					}).blur(function(){
						var $this = $(this);
						if ($this.val() != EBCMS.[namespace].sortvalue) {
							EBCMS.ACT.togglefield('{:U('Group/resort')}',$this.data('id'),$this.val(),'[namespace]','resort');
						};
					});
				},
			});
		};
		EBCMS.[namespace].save = function save(id){
			EBCMS.CORE.get({
				url:'{:U('Group/save')}',
				queryParams:{
					id:id,
				},
				target:'#lgModal .modal-content',
				loadAfter:function(data,p){
					$('#lgModal').modal('toggle');
				},
			});
		};
		EBCMS.[namespace].add = function add(pid){
			EBCMS.CORE.get({
				url:'{:U('Group/add')}',
				queryParams:{
					pid:pid,
				},
				target:'#lgModal .modal-content',
				loadAfter:function(data,p){
					$('#lgModal').modal('toggle');
				},
			});
		};
		/*分配用户组*/
		EBCMS.[namespace].rule = function rule(id){
			EBCMS.CORE.get({
				url:'{:U('Group/rule')}',
				queryParams:{
					id:id,
				},
				target:'#lgModal .modal-content',
				loadAfter:function(data,p){
					$('#lgModal').modal('toggle');
				},
			});
		};
		/*分配功能*/
		EBCMS.[namespace].menu = function menu(id){
			EBCMS.CORE.get({
				url:'{:U('Group/menu')}',
				queryParams:{
					id:id,
				},
				target:'#lgModal .modal-content',
				loadAfter:function(data,p){
					$('#lgModal').modal('toggle');
				},
			});
		};
		EBCMS.[namespace].refresh();
	});
</script>

<div class="header">
	<div class="header-title" onclick="EBCMS.[namespace].refresh();">角色管理</div>
	<div class="btn-group pull-right" role="group" aria-label="...">
		<a class="btn btn-success" href="javascript:EBCMS.[namespace].add()">添加</a>
	</div>
</div>
<div class="body" style="bottom:0px;" id="[namespace]_main">
</div>

<script id="[namespace]-table" type="text/html">
	<table class="table table-bordered">
		<tbody>
			<tr>
				<th>id</th>
				<th>名称</th>
				<th>排序</th>
				<th style="width:240px;">操作</th>
			</tr>
			{{each groups as rows group}}
			<tr>
				<th colspan="4">{{group}}</th>
			</tr>
				{{include '[namespace]-table-item' rows}}
			{{/each}}
		</tbody>
	</table>
</script>
<script id="[namespace]-table-item" type="text/html">
	{{each rows as v n}}
		<tr>
			<td>{{v.id}}</td>
			<td>{{v.levelstr}}{{v.title}}</td>
			<td><input value="{{v.sort}}" class="input_sort" data-id="{{v.id}}"></td>
			<td>
				<div class="btn-group btn-group-xs" role="group" aria-label="...">
				{{if access(0,'[namespace]','lock')}}
					{{if v.locked==1}}
					<a class="btn btn-info" href="javascript:EBCMS.ACT.lock('{:U('Group/lock')}','{{v.id}}','0','[namespace]');">已锁</a>
					{{else}}
					<a class="btn btn-info" href="javascript:EBCMS.ACT.lock('{:U('Group/lock')}','{{v.id}}','1','[namespace]');">未锁</a>
					{{/if}}
				{{/if}}
				{{if access(v.locked,'[namespace]','status')}}
					{{if v.status==1}}
					<a class="btn btn-info" href="javascript:EBCMS.ACT.status('{:U('Group/status')}','{{v.id}}','0','[namespace]');">已审</a>
					{{else}}
					<a class="btn btn-info" href="javascript:EBCMS.ACT.status('{:U('Group/status')}','{{v.id}}','1','[namespace]');">未审</a>
					{{/if}}
				{{/if}}
				{{if access(v.locked,'[namespace]','save')}}
				<a class="btn btn-info" href="javascript:EBCMS.[namespace].save('{{v.id}}');">编辑</a>
				{{/if}}
				{{if access(v.locked,'[namespace]','rule')}}
				<a class="btn btn-info" href="javascript:EBCMS.[namespace].rule('{{v.id}}');">权限</a>
				{{/if}}
				{{if access(v.locked,'[namespace]','menu')}}
				<a class="btn btn-info" href="javascript:EBCMS.[namespace].menu('{{v.id}}');">功能</a>
				{{/if}}
				{{if access(v.locked,'[namespace]','delete')}}
				<a class="btn btn-info" href="javascript:EBCMS.ACT.del('{:U('Group/delete')}','{{v.id}}','[namespace]');">删除</a>
				{{/if}}
				</div>
			</td>
		</tr>
		{{if v.rows}}
			{{include '[namespace]-table-item' v}}
		{{/if}}
	{{/each}}
</script>